package org.vz.product.collect.maintain.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vz.product.collect.maintain.domain.MessageChannelEntity;
import org.vz.product.collect.maintain.service.DistributedService;
import org.vz.product.collect.maintain.service.GetNamesService;
import org.vz.product.collect.maintain.service.GetServiceByIdAndDayService;
import org.vz.product.collect.maintain.service.LoadBalancingService;
import org.vz.product.collect.maintain.service.MessageChannelService;
import org.vz.product.collect.maintain.service.PlatformOverviewService;
import org.vz.product.collect.maintain.service.ServiceVisualMonitoringService;
import org.vz.product.collect.maintain.util.StringUtil;

@Controller
@RequestMapping("/message")
public class MessageController {
	@Autowired
	private PlatformOverviewService platformOverviewService;

	@Autowired
	private LoadBalancingService loadBalancingService;

	@Autowired
	private MessageChannelService messageChannelService;

	@Autowired
	private ServiceVisualMonitoringService serviceVisualMonitoringService;

	@Autowired
	private GetServiceByIdAndDayService getServiceByIdAndDayService;

	@Autowired
	private DistributedService getDistributedService;

	 @Autowired
	 private GetNamesService getNamesService;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/*
	 * 平台总运行 不需要
	 */
	@RequestMapping("/platformOverview")
	@ResponseBody
	public String platformOverviewService(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 24);
			endTime = sdf.format(c.getTimeInMillis());
		}
		return StringUtil.toJsonString(platformOverviewService.getData(
				startTime, endTime));
	}

	/*
	 * 负载平衡
	 */
	@RequestMapping("/loadBalancing")
	@ResponseBody
	public String loadBalancingService(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 24);
			endTime = sdf.format(c.getTimeInMillis());
		}
		return StringUtil
				.toJsonString(loadBalancingService.getDataByOneOvers());
	}

	/*
	 * 消息通道详情
	 */
	@RequestMapping("/messageChannelResult")
	@ResponseBody
	public String messageChannelResultService(
//			@RequestParam(value = "pageNo", defaultValue = "1") long pageNo,
//			@RequestParam(value = "pageSize", defaultValue = "19") long pageSize,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			startTime = "2017-01-01 10:12:00";
			endTime = sdf.format(c.getTimeInMillis());
		}
//		PageQuery<MessageChannelEntity> pageQuery = new PageQuery<MessageChannelEntity>(
//				pageNo, pageSize);

		return StringUtil.toJsonString(messageChannelService.pageList(startTime, endTime));
	}

	/*
	 * 获取所有服务
	 */
	@RequestMapping("/ServiceVisualMonitoring")
	@ResponseBody
	public String ServiceVisualMonitoring(
			@RequestParam(value = "type", defaultValue = "0") int type,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			switch (type) {
			case 0:// 近一天
				startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
						* 23);
				endTime = sdf.format(c.getTimeInMillis() + 1000 * 60 * 60 * 1);
				break;
			case 1:// 近一小时
				startTime = sdf
						.format(c.getTimeInMillis() - 1000 * 60 * 60 * 1);
				endTime = sdf.format(c.getTimeInMillis() + 1000 * 60);
				break;
			case 2:// 近一个月
				startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
						* 24 * 15);
				endTime = sdf.format(c.getTimeInMillis());
				break;
			case 3:// 本年内
				Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
				ca.set(c.get(Calendar.YEAR), 0, 1);// 月份是从0开始的，所以11表示12月
				startTime = sdf.format(ca.getTimeInMillis());
				endTime = sdf.format(c.getTimeInMillis() + 1000 * 60 * 60 * 24
						* 28);
				break;
			default:
				break;
			}
		}
		return StringUtil.toJsonString(serviceVisualMonitoringService
				.getAllServiceByDay(startTime, endTime, type));
	}

	/*
	 * 根据ID获取所有服务
	 */
	@RequestMapping("/GetServiceByIdAndDayService")
	@ResponseBody
	public String GetServiceByIdAndDayService(
			@RequestParam(value = "type", defaultValue = "0") int type,
			@RequestParam(value = "serviceId", defaultValue = "") String serviceId,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (serviceId.equals("")) {
			serviceId = "ClinicService";
		}
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 24);
			endTime = sdf.format(c.getTimeInMillis());
		}
		return StringUtil.toJsonString(getServiceByIdAndDayService.getService(
				startTime, endTime, serviceId, type));
	}

	/*
	 * 分布式处理
	 */
	@RequestMapping("/getDistributedService")
	@ResponseBody
	public String getDistributedService(
			@RequestParam(value = "type", defaultValue = "0") int type,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime) {
		if (startTime.equals("") || endTime.equals("")) {
			Calendar c = Calendar.getInstance();
			endTime = sdf.format(c.getTimeInMillis());
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 24
					* 15);
		}
		return StringUtil.toJsonString(getDistributedService
				.getDistributedEntity(type, startTime, endTime));
	}
	
	/*
	 * 分布式处理
	 */
	@RequestMapping("/GetNamesService")
	@ResponseBody
	public String getServiceNames() {
		return StringUtil.toJsonString(getNamesService.getNames());
	}
}
